# movie_app/models.py
from django.db import models
from django.core.validators import URLValidator

class Movie(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField()
    release_year = models.PositiveIntegerField()
    # poster字段可以存储上传的图片或外部URL
    poster = models.CharField(max_length=500, blank=True)
    poster_image = models.ImageField(upload_to='posters/', blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

    @property
    def poster_url(self):
        """返回图片URL，优先使用上传的图片"""
        if self.poster_image:
            return self.poster_image.url
        return self.poster if self.poster else None
